#include "stdafx.h"

#include <atlstr.h>
#include "..\ResultReporter.h"

namespace Fast
{

TESTCLASS(ResultReporterTests)
{
	class Output
	{
		CString m_result;
	public:
		void operator()(const CString& cs) { m_result += cs; }
		const CString& GetString() const { return m_result; }
		void Reset() { m_result.Empty(); }
	};

	TESTMETHOD(NoTestsRun)
	{
		Output output;
		{
			ResultReporterT<Output> rr(output);
			TDD_VERIFY(0 == rr.TestsRun());
			TDD_VERIFY(0 == rr.TestsFailed());
		}
		TDD_VERIFY(output.GetString() == L"No tests were run!!!\n");
	}

	TESTMETHOD(OneSuccessfulTest)
	{
		Output output;
		{
			TDD::UnitTestInfo uti("class", "test");
			ResultReporterT<Output> rr(output);
			rr.ForEachTest(uti);
		}
		TDD_VERIFY(output.GetString() == L"0 failures out of 1 test run\n");
	}
	TESTMETHOD(TwoSuccessfulTests)
	{
		Output output;
		{
			TDD::UnitTestInfo uti("class", "test");
			ResultReporterT<Output> rr(output);
			rr.ForEachTest(uti);
			rr.ForEachTest(uti);
		}
		TDD_VERIFY(output.GetString() == L"0 failures out of 2 tests run\n");
	}
	TESTMETHOD(OneFailureOutOfTwoTests)
	{
		Output output;
		{
			TDD::UnitTestInfo uti("class", "test");
			TDD::TestFailure tf(&uti, 10, "myfile.h", "some error");

			ResultReporterT<Output> rr(output);
			rr.ForEachTest(uti);
			rr.ForEachTest(uti);
			rr.ForEachFailure(tf);
			output.Reset(); // interested in dtor only

			TDD_VERIFY(2 == rr.TestsRun());
			TDD_VERIFY(1 == rr.TestsFailed());
		}
		TDD_VERIFY(output.GetString() == L"1 failure out of 2 tests run\n");
	}
	TESTMETHOD(TwoFailuresOutOfTwoTests)
	{
		Output output;
		{
			TDD::UnitTestInfo uti("class", "test");
			TDD::TestFailure tf(&uti, 10, "myfile.h", "some error");

			ResultReporterT<Output> rr(output);
			rr.ForEachTest(uti);
			rr.ForEachFailure(tf);
			rr.ForEachTest(uti);
			rr.ForEachFailure(tf);
			output.Reset(); // interested in dtor only
		}
		TDD_VERIFY(output.GetString() == L"2 failures out of 2 tests run\n");
	}
	TESTMETHOD(OneFailuresOutOfOneTest)
	{
		Output output;
		{
			TDD::UnitTestInfo uti("class", "test");
			TDD::TestFailure tf(&uti, 10, "myfile.h", "some error");

			ResultReporterT<Output> rr(output);
			rr.ForEachTest(uti);
			rr.ForEachFailure(tf);
			output.Reset(); // interested in dtor only
		}
		TDD_VERIFY(output.GetString() == L"1 failure out of 1 test run\n");
	}

	TESTMETHOD(VerifyFailure)
	{
		Output output;

		TDD::UnitTestInfo uti("class", "test");
		TDD::TestFailure tf(&uti, 10, "myfile.h", "some error");

		ResultReporterT<Output> rr(output);
		rr.ForEachTest(uti);
		rr.ForEachFailure(tf);

		TDD_VERIFY(output.GetString() == L"Failure in class.test -\nmyfile.h(10) : ERROR : \"some error\"\n");
	}
};

}
